/*
 *  Copyright 2011 yura.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.vcforms.template.archive;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.opu.vcforms.template.TxtTemplate;

/**
 *
 * @author yura
 */
public class TextArchiveTemplate extends AbstractArchiveTemplate {
    public static final String TXT_TEMPLATE_NAME = "TextTemplate.xml";

    private TxtTemplate template = new TxtTemplate();

    public TxtTemplate getTemplate() {
        return template;
    }

    public void setTemplate(TxtTemplate template) {
        this.template = template;
    }

    @Override
    public void onLoad(String name, byte[] data) throws IOException {
        if(TXT_TEMPLATE_NAME.equals(name)){
            if (template == null){
                template = new TxtTemplate();
            }

            template.load(new ByteArrayInputStream(data));
        }
    }

    @Override
    public byte[] onStore(String name, byte[] data) throws IOException {
        if(TXT_TEMPLATE_NAME.equals(name)){
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            template.store(bout);
            return bout.toByteArray();
        } else {
            return super.onStore(name, data);
        }
    }

    @Override
    public String[] requred() {
        return new String[]{TXT_TEMPLATE_NAME};
    }

}
